import axios from 'axios';
import Exif from '../pages/InformationCollection/exif';

export const imgPreview = (file) => {
  let Orientation;

  Exif.getData(file, function() {
    Orientation = Exif.getTag(this, "Orientation");
  });

  if (!file || !window.FileReader) return;
  if (/^image/.test(file.type)) {
    let reader = new FileReader();
    reader.readAsDataURL(file);

    return new Promise((resolve, reject)=>{
     reader.onloadend = function() {
        let fileName;
        let result = this.result;
        let img = new Image();
        img.src = result;

        if (this.result.length <= 100 * 1024) {
          fileName = this.result;
          // const headfimg = this.result;
          // postImg(headfimg, cTransactionNbr, wfType);
          resolve(fileName);
        } else {
          img.onload = function() {
            let data = compress(img, Orientation);
            fileName = data;
            // const headfimg = data;
            // postImg(headfimg, cTransactionNbr, wfType);
            resolve(fileName);
          };
        }
      }
    });
  }
}

export const postImg = (img, cTransactionNbr, wfType, name) => {
  let formData = new FormData();
  formData.append("fileName", img);
  if (cTransactionNbr) {
    formData.append("cTransactionNbr", cTransactionNbr);
  } else {
    formData.append("cTransactionNbr", '');
  }
  formData.append("wfType", wfType);
  formData.append("originName", name);
  const options = {
    url: "/api/hr/process/uploadWfFile",
    method: "post",
    data: formData
  };
  return new Promise((resolve, reject)=>{
    axios(options)
      .then(res => {
        resolve(res.data);
      })
      .catch(res => {
        reject(res.data);
      });
  })
}

const rotateImg = (img, direction, canvas) => {
  const min_step = 0;
  const max_step = 3;
  if (img == null) return;

  let height = img.height;
  let width = img.width;
  let step = 2;
  if (step == null) {
    step = min_step;
  }
  if (direction == "right") {
    step++;

    step > max_step && (step = min_step);
  } else {
    step--;
    step < min_step && (step = max_step);
  }

  let degree = step * 90 * Math.PI / 180;
  let ctx = canvas.getContext("2d");
  switch (step) {
    case 0:
      canvas.width = width;
      canvas.height = height;
      ctx.drawImage(img, 0, 0);
      break;
    case 1:
      canvas.width = height;
      canvas.height = width;
      ctx.rotate(degree);
      ctx.drawImage(img, 0, -height);
      break;
    case 2:
      canvas.width = width;
      canvas.height = height;
      ctx.rotate(degree);
      ctx.drawImage(img, -width, -height);
      break;
    case 3:
      canvas.width = height;
      canvas.height = width;
      ctx.rotate(degree);
      ctx.drawImage(img, -width, 0);
      break;
  }
}

const compress = (img, Orientation) => {
  let canvas = document.createElement("canvas");
  let ctx = canvas.getContext("2d");

  let tCanvas = document.createElement("canvas");
  let tctx = tCanvas.getContext("2d");
  let initSize = img.src.length;
  let width = img.width;
  let height = img.height;

  let ratio;
  if ((ratio = width * height / 4000000) > 1) {
    ratio = Math.sqrt(ratio);
    width /= ratio;
    height /= ratio;
  } else {
    ratio = 1;
  }
  canvas.width = width;
  canvas.height = height;

  ctx.fillStyle = "#fff";
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  let count;
  if ((count = width * height / 1000000) > 1) {
    count = ~~(Math.sqrt(count) + 1);

    let nw = ~~(width / count);
    let nh = ~~(height / count);
    tCanvas.width = nw;
    tCanvas.height = nh;
    for (let i = 0; i < count; i++) {
      for (let j = 0; j < count; j++) {
        tctx.drawImage(
          img,
          i * nw * ratio,
          j * nh * ratio,
          nw * ratio,
          nh * ratio,
          0,
          0,
          nw,
          nh
        );
        ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
      }
    }
  } else {
    ctx.drawImage(img, 0, 0, width, height);
  }

  if (Orientation != "" && Orientation != 1) {
    switch (Orientation) {
      case 6:
        rotateImg(img, "left", canvas);
        break;
      case 8:
        rotateImg(img, "right", canvas);
        break;
      case 3:
        rotateImg(img, "right", canvas); //转两次
        rotateImg(img, "right", canvas);
        break;
    }
  }

  let ndata = canvas.toDataURL("image/jpeg", 0.1);
  tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
  //const base64Data = await this.blobToDataURL(ndata)
  // console.log(typeof ndata);
  return ndata;
}
